<?php
class CategoryAction extends CommonAction {
	//过滤查询字段
	protected $config=array('app_type'=>'common','action_auth'=>array('set_tag'=>'admin'));
	function _search_filter(&$map) {
		$map['name'] = array('like', "%" . $_POST['name'] . "%");
		$map['letter'] = array('like', "%" . $_POST['letter'] . "%");
		$map['is_del'] = array('eq', '0');
		if (!empty($_POST['tag'])) {
			$map['group'] = $_POST['tag'];
		}
		$map['is_del'] = array('eq', '0');
	}

	function index() {
    import("@.ORG.Page");       //导入分页类
		$model = M("Category");
    $search_sql = '1=1';
    //print_r($_GET);
    $cate = $_GET['cate'];
    if($_GET['cate']){
      $search_sql .= " and cate = '".$_GET['cate']."'"; 
    }
    //print_r($search_sql);
    if($_POST['search_txt']){
      $search_sql .= " and name like '%".$_POST['search_txt']."%'";
    }
    $list = $model->where($search_sql)->select();
    $param = array(
      'result'=>$list,            //分页用的数组或sql
      'listvar'=>'list',            //分页循环变量
      'listRows'=>10,            //每页记录数
      'parameter'=>'',//url分页后继续带的参数
      'target'=>'content',    //ajax更新内容的容器id，不带#
      'pagesId'=>'page',        //分页后页的容器id不带# target和pagesId同时定义才Ajax分页
      'template'=>'Category:ajaxlist',//ajax更新模板
    );
    $this -> assign('cate',$cate);
    $this -> page($param);
		$this -> display();
	}


  function tag_list() {
    $model = D("SystemTag");
    $tag_list = $model -> get_list('id,name');
    $this -> assign("tag_list", $tag_list);
  }

  function add() {
    $this -> assign('cate',$_GET['cate']);
    $this -> display();
  }

  //function edit() {
    //$model = M('Category');
    //$this -> assign('cate',$_GET['cate']);
    //$this -> assign('vo',$model->find($id));
    //$this -> display();
  //}

  function insert() {
    $ajax = $_POST['ajax'];
    $model = D('Category');
    if (false === $model -> create()) {
      $this -> error($model -> getError());
    }
    $model -> __set('letter', get_letter($model -> __get('name')));
    $model -> __set('user_id', get_user_id());
    //保存当前数据对象
    $list = $model -> add();
    if ($list !== false) {//保存成功
      if ($ajax || $this -> isAjax())
        $this -> ajaxReturn($list, "新增成功", 1);
      $this -> assign('jumpUrl', get_return_url());
      $this -> success('新增成功!');
    } else {
      //失败提示
      $this -> error('新增失败!');
    }
  }

  function update() {
    $ajax = $_POST['ajax'];
    $id = $_POST['id'];
    $model = D("Category");
    if (false === $model -> create()) {
      $this -> error($model -> getError());
    }
    $model -> __set('letter', get_letter($model -> __get('name')));
    // 更新数据
    $list = $model -> save();
    if (false !== $list) {
      //成功提示
      $this -> assign('jumpUrl', get_return_url());
      $this -> success('编辑成功!');
    } else {
      //错误提示
      $this -> error('编辑失败!');
    }
  }

  function del() {
    if (!empty($_POST['customer_id'])) {
      $model = M("Category");
      $Category_list = $_POST['customer_id'];
      if (!is_array($Category_list)) {
        $Category_list = explode(",", $Category_list);
      }
      $where['id'] = array('in', $Category_list);
      $where['user_id'] = get_user_id();
      $model -> where($where) -> delete();
      $model = D("SystemTag");
      $result = $model -> del_data_by_row($_POST['customer_id'], 'Category');
    };
    if ($result !== false) {//保存成功
      if ($ajax || $this -> isAjax())
        $this -> ajaxReturn($list, "操作成功", 1);
      $this -> assign('jumpUrl', get_return_url());
      $this -> success('操作成功!');
    } else {
      //失败提示
      $this -> error('操作失败!');
    }
  }

  function read() {
      $model = M('Category');
      $id = $_REQUEST[$model -> getPk()];
      $vo = $model -> getById($id);
      $this -> assign('vo', $vo);
      $this -> display();
  }

  function set_tag() {
    if (!empty($_POST['customer_id'])) {
      $model = D("SystemTag");
      $model -> del_data_by_row($_POST['customer_id'], 'customer');
      if (!empty($_POST['tag'])) {
        $result = $model -> set_tag($_POST['customer_id'], $_POST['tag'], "customer");
      }
    };
    if (!empty($_POST['new_tag'])) {
      $model = M("SystemTag");
      $model -> module = MODULE_NAME;
      $model -> name = $_POST['new_tag'];
      $model -> status = 1;
      $model -> user_id = get_user_id();
      $new_tag_id = $model -> add();
      if (!empty($_POST['customer_id'])) {
        $model = D("SystemTag");
        $result = $model -> set_tag($_POST['customer_id'], $new_tag_id, "customer");
      }
    };
    if ($result !== false) {//保存成功
      if ($ajax || $this -> isAjax())
        $this -> ajaxReturn($list, "操作成功", 1);
      $this -> assign('jumpUrl', get_return_url());
      $this -> success('操作成功!');
    } else {
      //失败提示
      $this -> error('操作失败!');
    }
  }
}
?>
